package com.erp.module.xiSuZhiXiang

import android.content.Context
import com.erp.bean.XiSuZhiXiangBean
import com.erp.Interface.OnUserDataInterface
import com.erp.Myapp
import com.erp.pull.BasePullP
import com.erp.util.Httputil
import com.erp.util.JsonUtil
import com.erp.module.jiehao.FragmentKJieHao
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import java.util.*

/**
 * Created by Administrator on 2019/3/19/019.
 * 作者：刘洋
 */
class IXiSuZhiXiangP(var V: XiSuZhiXiangContract.XisuZhiXiangView,var  context : Context,var list :MutableList<XiSuZhiXiangBean>) : BasePullP(), XiSuZhiXiangContract.XiSuZhiXiangP {
    var md =XiSuZhiXiangModel()
    var sousuobean = fenpeiSouSuoBean()
    init {
        PullV = V
    }
    override fun getData(page: Int, pagesize: Int, onUserDataInterface: OnUserDataInterface) {
        md.getJieHao(context,page,pagesize,sousuobean,onUserDataInterface)
    }

    override fun jsondata(data: String?) {
        try{
            val js = JSONArray(data)
            for(i in 0 until js.length()){
                val jo=js[i] as JSONObject
                var bean= XiSuZhiXiangBean()
                bean.id= JsonUtil.getvolue(jo,"id")
                bean.batch_name= JsonUtil.getvolue(jo,"batch_name")
                bean.onum= JsonUtil.getvolue(jo,"onum")
                bean.cnum= JsonUtil.getvolue(jo,"cnum")
                bean.distributed_target= JsonUtil.getvolue(jo,"distributed_target")
                bean.is_distributed= JsonUtil.getvolue(jo,"is_distributed")
                bean.assign_name= JsonUtil.getvolue(jo,"assign_name")
                bean.assign_time= JsonUtil.getvolue(jo,"assign_time")
                bean.pcode= JsonUtil.getvolue(jo,"pcode")
                bean.ctime= JsonUtil.getvolue(jo,"ctime")
                val jsm = JsonUtil.getjsvolue(jo,"material")
                for(j in 0 until jsm.length()){
                    var m= XiSuZhiXiangBean.material()
                    val jom=jsm[j] as JSONObject
                    m.name= JsonUtil.getvolue(jom,"name")
                    m.size= JsonUtil.getvolue(jom,"size")
                    m.counts= JsonUtil.getvolue(jom,"counts")
                    bean.materials.add(m)
                }
                list.add(bean)
            }
        }catch (e : JSONException){
            e.printStackTrace()
        }
    }

    override fun listClean() {
        list.clear()
    }

    override fun fenPeiJieHao(jieHaoKBean: String, feipei: String, isfenpei: String) {
        V.fenpeijiehao()
        var jo=JSONObject()
        try {
            if(isfenpei=="1") {
                jo.put("distributed_target",feipei)
            }
            jo.put("is_distributed",isfenpei)
        }catch ( e :JSONException){
            e.printStackTrace()
        }
        md.setjiehao(context,jo.toString(),jieHaoKBean,object : OnUserDataInterface{
            override fun onUserData(data: String) {
                V.fenPeiJieHaoOk(data)
                if(isfenpei=="1"){
                    Myapp.editor.putLong(FragmentKJieHao.FENPEITIME, Date().time / 1000)
                    Myapp.editor.commit()
                }
            }

            override fun onNot(throwable: String) {
                V.fenPeiJieHaoNo(throwable)
            }

        })
    }

    override fun souSuoZi(suosuo: String) {
        sousuobean.sousuo=suosuo
    }

    override fun getWuliaoLeibie() {
        var zhiliangid: MutableList<String> = ArrayList()
        var zhiliangname: MutableList<String> = ArrayList()
        Httputil.getWuLiao_LeiBie_DaiMa(context,object : OnUserDataInterface{
            override fun onUserData(data: String?) {
                if(data==null){
                    return
                }
                val jo = JSONObject(data)
                val keys = jo.keys()
                while (keys.hasNext()) {
                    val key = keys.next()
                    zhiliangid.add(key)
                    zhiliangname.add(JsonUtil.getvolue(jo, key))
                }
                V.wuliaoLeibieOk(zhiliangid,zhiliangname)
            }

            override fun onNot(throwable: String?) {
                V.wuliaoLeibieNo()
            }

        })
    }

    override fun wuliaoLeiBieId(wuliaoId: String) {
        sousuobean.wuliaoId=wuliaoId
    }

    override fun shijiansousuo(time_category: String, sterttime: String, endtime: String) {
        var c =""
        when(time_category){
            "上传时间" -> {
                c = "pcd.ctime"
            }
            "分配时间" -> {
                c = "pcd.assign_time"
            }
        }
        sousuobean.time_category=c
        sousuobean.sterttime=sterttime
        sousuobean.endtime=endtime
    }

    override fun souSousuoOrder(order: String) {
        sousuobean.order=order
    }

    class fenpeiSouSuoBean{
        var sousuo = ""
        var order = ""
        var time_category = ""
        var sterttime =""
        var endtime =""
        var wuliaoId=""
    }
}